package com.ruoyi.web.controller.prt;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.prt.domain.PrtShareDoc;
import com.ruoyi.prt.domain.vo.PrtShareDocVO;
import com.ruoyi.prt.service.IPrtShareDocService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.file.FileUploadUtils.getExtension;

/**
 * 分享文档Controller
 *
 * @author xiaobo Yan
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/prt/sharedoc")
public class PrtShareDocController extends BaseController
{
    @Autowired
    private IPrtShareDocService prtShareDocService;

    /**
     * 查询分享文档列表
     */
    @PreAuthorize("@ss.hasPermi('system:doc:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrtShareDoc prtShareDoc)
    {
        startPage();
        List<PrtShareDoc> list = prtShareDocService.selectPrtShareDocList(prtShareDoc);
        return getDataTable(list);
    }

    /**
     * 导出分享文档列表
     */
    @PreAuthorize("@ss.hasPermi('system:doc:export')")
    @Log(title = "分享文档", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PrtShareDoc prtShareDoc)
    {
        List<PrtShareDoc> list = prtShareDocService.selectPrtShareDocList(prtShareDoc);
        ExcelUtil<PrtShareDoc> util = new ExcelUtil<PrtShareDoc>(PrtShareDoc.class);
        return util.exportExcel(list, "doc");
    }

    /**
     * 获取分享文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:doc:query')")
    @GetMapping(value = "/{docId}")
    public AjaxResult getInfo(@PathVariable("docId") String docId)
    {
        return AjaxResult.success(prtShareDocService.selectPrtShareDocById(docId));
    }

    /**
     * 新增分享文档
     */
//    @PreAuthorize("@ss.hasPermi('system:doc:add')")
//    @Log(title = "分享文档", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody PrtShareDoc prtShareDoc)
//    {
//        return toAjax(prtShareDocService.insertPrtShareDoc(prtShareDoc));
//    }

    /**
     * 修改分享文档
     */
    @PreAuthorize("@ss.hasPermi('system:doc:edit')")
    @Log(title = "分享文档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrtShareDoc prtShareDoc)
    {
        return toAjax(prtShareDocService.updatePrtShareDoc(prtShareDoc));
    }

    /**
     * 删除分享文档
     */
    @PreAuthorize("@ss.hasPermi('system:doc:remove')")
    @Log(title = "分享文档", businessType = BusinessType.DELETE)
	@DeleteMapping("/{docIds}")
    public AjaxResult remove(@PathVariable String[] docIds)
    {
        return toAjax(prtShareDocService.deletePrtShareDocByIds(docIds));
    }

    /**
     * 后端分享文档
     */
//    @PreAuthorize("@ss.hasPermi('system:doc:add')")
    @Log(title = "后端分享文档", businessType = BusinessType.UPLOAD_FILE)
    @PostMapping("/addFile")
    public AjaxResult addFile(@RequestParam(value="file") MultipartFile file)
    {
        System.out.println("getOriginalFilename:"+file.getOriginalFilename());
        System.out.println("file:"+file);
        try {
            PrtShareDoc prtShareDoc = upShareFile(file, file.getOriginalFilename());
            prtShareDoc.setAuditor(SecurityUtils.getUserId());
            prtShareDoc.setAuditorName(SecurityUtils.getUsername());
            prtShareDoc.setAuditorTime(new Date());
            prtShareDoc.setAuditStatus("1");
//            System.out.println("11111111");
            prtShareDocService.insertPrtShareDoc(prtShareDoc);
            return AjaxResult.success().setData(prtShareDoc);
        } catch (Exception e) {
//            System.out.println("2222222222222");
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 客户端分享文档
     */
    @Log(title = "客户端分享文档", businessType = BusinessType.UPLOAD_FILE)
    @PostMapping("/addShareFile")
    @PreAuthorize("@ss.hasRole('mobile')")
    public AjaxResult addShareFile(MultipartFile file, @Param("filename") String filename) {
        try {
            PrtShareDoc prtShareDoc = upShareFile(file, filename);
            prtShareDoc.setAuditStatus("0");
            prtShareDocService.insertPrtShareDoc(prtShareDoc);
            return AjaxResult.success().setData(prtShareDoc);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasRole('mobile')")
    @Log(title = "获取共享文档", businessType = BusinessType.SELECT)
    @GetMapping("/getShareFiles")
    public AjaxResult getShareFiles(PrtShareDocVO params) {
        return AjaxResult.success().setData(prtShareDocService.selectPrtShareDocList(params));
    }

    public PrtShareDoc upShareFile(MultipartFile file, String docName) throws IOException {
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
        String extension = getExtension(file);
        PrtShareDoc prtShareDoc = new PrtShareDoc();

        prtShareDoc.setDocName(docName);
        prtShareDoc.setCreated(SecurityUtils.getUserId());
        prtShareDoc.setSaveName(filePath);
        prtShareDoc.setAddress(filePath);
        prtShareDoc.setDocFormat(extension);
        prtShareDoc.setContentType(file.getContentType());
        prtShareDoc.setIsEnable(1);
        prtShareDoc.setSize(file.getSize());
        prtShareDoc.setCreatedName(SecurityUtils.getUsername());
        prtShareDoc.setSaveType("file");
        prtShareDoc.setDocId(StringUtils.getUUID());
        prtShareDoc.setCreatedTime(new Date());
        return prtShareDoc;
    }
}
